package com.manor.common.glide;

import android.content.Context;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.AppGlideModule;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class YMyAppGlideModule extends AppGlideModule {
    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        //1.根据设备信息计算出合适的内存缓存大小
        MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
                .setMemoryCacheScreens(2)
                .build();
        builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));
        //2.也可以直接覆盖缓存大小
        int memoryCacheSize = 1024*1024*15; //15M内存缓存
        builder.setMemoryCache(new LruResourceCache(memoryCacheSize));
        //3.提供自己的MemoryCache实现
        builder.setMemoryCache(new MemoryCache() {
            @Override
            public long getCurrentSize() {
                return 0;
            }

            @Override
            public long getMaxSize() {
                return 0;
            }

            @Override
            public void setSizeMultiplier(float multiplier) {

            }

            @Nullable
            @Override
            public Resource<?> remove(@NonNull Key key) {
                return null;
            }

            @Nullable
            @Override
            public Resource<?> put(@NonNull Key key, @Nullable Resource<?> resource) {
                return null;
            }

            @Override
            public void setResourceRemovedListener(@NonNull ResourceRemovedListener listener) {

            }

            @Override
            public void clearMemory() {

            }

            @Override
            public void trimMemory(int level) {

            }

        });
    }
}
